/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ua.yandex.skipass;

import java.util.Calendar;

/**
 *
 * @author Neophron
 */
public class WorkDayDayPass implements Pass {

    private static final PassType TYPE = PassType.WORKDAYDAY;

    private final Calendar startDay;

    private final DayCount dayCount;

    private final int id;

    private WorkDayDayPass(int id, DayCount dayCount) {
        this.id = id;
        this.dayCount = dayCount;
        startDay = dayCount.getStartDay();
    }

    @Override
    public int getId() {
        return id;
    }

    @Override
    public PassType getType() {
        return TYPE;
    }

    @Override
    public boolean allowTrip(Calendar curDay) {
        switch (dayCount) {
            case FIRSTHALFDAY:
                if (curDay.get(Calendar.HOUR_OF_DAY) >= 9
                        && curDay.get(Calendar.HOUR_OF_DAY) <= 12
                        && curDay.get(Calendar.DAY_OF_YEAR)
                        == startDay.get(Calendar.DAY_OF_YEAR)) {
                    return true;
                }
                return false;
            case SECONDHALFDAY:
                if (curDay.get(Calendar.HOUR_OF_DAY) >= 13
                        && curDay.get(Calendar.HOUR_OF_DAY) <= 16
                        && curDay.get(Calendar.DAY_OF_YEAR)
                        == startDay.get(Calendar.DAY_OF_YEAR)) {
                    return true;
                }
                return false;
            case ONEDAY:
                if (curDay.get(Calendar.DAY_OF_YEAR)
                        == startDay.get(Calendar.DAY_OF_YEAR)) {
                    return true;
                }
                return false;
            case TWODAYS:
                if (curDay.get(Calendar.DAY_OF_YEAR)
                        == startDay.get(Calendar.DAY_OF_YEAR)
                        || startDay.get(Calendar.DAY_OF_YEAR)
                        == prevDate(curDay).get(Calendar.DAY_OF_YEAR)) {
                    return true;
                }
                return false;
            case FIVEDAYS:
                Calendar day = curDay;
                for (int i = 0; i < 5; i++) {
                    if (day.get(Calendar.DAY_OF_YEAR)
                            == startDay.get(Calendar.DAY_OF_YEAR)) {
                        return true;
                    }
                    day = prevDate(day);
                }
                return false;

        }
        return false;
    }

    private Calendar prevDate(Calendar myDate) {
        Calendar date = myDate;
        boolean isWeekend = date.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY
                || date.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY;
        date.add(Calendar.DATE, -1);
        if (isWeekend) {
            if (date.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
                date.add(Calendar.DATE, -5);
            }
        } else {
            if (date.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                date.add(Calendar.DATE, -2);
            }
        }
        return date;
    }

    public static WorkDayDayPass createWorkDayDayPass(int id,
            DayCount dayCount) {
        if (dayCount.getStartDay().get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY
                || dayCount.getStartDay().get(Calendar.DAY_OF_WEEK)
                == Calendar.MONDAY) {
            throw new IllegalArgumentException();
        }
        return new WorkDayDayPass(id, dayCount);
    }
}
